﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using System.Threading;

using PortalIntranet.InternalComponents.Configuration;
using PortalIntranet.InternalComponents.Security;
using PortalIntranet.BusinessComponents.Seguranca;
using PortalIntranet.BusinessComponents.Administracao;
using PortalIntranet.ObjectComponents.Comun;
using PortalIntranet.DatabaseComponents.Administracao;
using PortalIntranet.ObjectComponents.Administracao;
using PortalIntranet.ObjectComponents.Seguranca;

namespace IntranetApplication
{
	// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
	// visit http://go.microsoft.com/?LinkId=9394801
	public class MvcApplication : System.Web.HttpApplication
	{
		#region Application Events
		protected void Application_Start()
		{
			AreaRegistration.RegisterAllAreas();
			WebApiConfig.Register(GlobalConfiguration.Configuration);
			FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
			RouteConfig.RegisterRoutes(RouteTable.Routes);
		}

		protected void Application_BeginRequest(object sender, EventArgs e)
		{
			HttpContext.Current.Response.AddHeader("ACCESS-CONTROL-ALLOW-ORIGIN", "*");
		}

		public void Application_AuthenticateRequest(Object sender, EventArgs e)
		{
			if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
			{
				var cache = HttpContext.Current.Cache.Get(AppSettings.AuthCacheName);
				if (cache == null) return;
				UsuarioInfo usuario = cache as UsuarioInfo;
				if (usuario == null) return;
				CustomPrincipal principal = new CustomPrincipal(usuario.Email);
				principal.Usuario = usuario;
				principal.Empresa = new EmpresaInfo(EmpresasBll.Obter(usuario.EmpresaId));
				principal.Menus = CriarMenus(usuario.EmpresaId, usuario.Id);
				Thread.CurrentPrincipal = principal;
				HttpContext.Current.User = principal;
			}
		}
		#endregion

		#region Functions
		public List<MenuItemInfo> CriarMenus(int empresa, int usuario)
		{
			List<Departamento> departamentos = DepartamentosBll.ListarDepartamentos(empresa);
			List<MenuItemInfo> menus = new List<MenuItemInfo>();
			MenuItemInfo menuitem = null;
			MenuItemInfo submenuitem = null;

			menus.Add(new MenuItemInfo()
			{
				Id = -1,
				Nome = "Home",
				Slug = "home",
				Uri = new UriInfo("Home", "Home"),
				SubMenus = new List<MenuItemInfo>()
			});

			foreach (var menu in departamentos.Where(c => c.DepartamentoPaiId == null && !c.Home))
			{
				menuitem = new MenuItemInfo();
				menuitem.Id = menu.Id;
				menuitem.Slug = menu.Slug;
				menuitem.Nome = menu.Nome;

				menuitem.Uri = DepartamentosBll.ObterUri(menu);
				menuitem.SubMenus = new List<MenuItemInfo>();

				foreach (var submenu in departamentos.Where(c => c.DepartamentoPaiId == menu.Id && !c.Home))
				{
					submenuitem = new MenuItemInfo();
					submenuitem.Id = submenu.Id;
					submenuitem.Nome = submenu.Nome;
					submenuitem.Slug = submenu.Slug;
					submenuitem.Uri = DepartamentosBll.ObterUri(submenu);
					submenuitem.SubMenus = new List<MenuItemInfo>();

					foreach (var item in departamentos.Where(c => c.DepartamentoPaiId == submenu.Id && !c.Home))
					{
						submenuitem.SubMenus.Add(new MenuItemInfo()
						{
							Id = item.Id,
							Nome = item.Nome,
							Slug = item.Slug,
							Uri = DepartamentosBll.ObterUri(item),
							SubMenus = new List<MenuItemInfo>()
						});
					}

					menuitem.SubMenus.Add(submenuitem);
				}

				menus.Add(menuitem);
			}

			return menus;
		}
		#endregion
	}
}